<template>
  <div>
    <Form ref="form" :model="formItem" :rules="formItemRules" :label-width="135">
      <FormItem label="任务名称" prop="jobName">
        <Input :disabled="formItem.isEdit" v-model="formItem.jobName" placeholder="请输入内容"></Input>
      </FormItem>
      <FormItem label="任务类型" prop="jobType">
        <Select v-model="formItem.jobType">
          <Option value="cron">cron任务(CronTrigger)</Option>
          <Option value="simple">简单任务(SimpleTrigger)</Option>
        </Select>
      </FormItem>
      <FormItem v-if="formItem.jobType === 'simple'" label="开始时间" prop="startTime">
        <DatePicker
          v-model="formItem.startTime"
          type="datetime"
          placeholder="开始时间"
          style="width: 100%"></DatePicker>
      </FormItem>
      <FormItem v-if="formItem.jobType === 'simple'" label="结束时间" prop="endTime">
        <DatePicker v-model="formItem.endTime" type="datetime" placeholder="结束时间" style="width: 100%"></DatePicker>
      </FormItem>
      <FormItem v-if="formItem.jobType === 'simple'" label="重复执行" prop="repeatCount">
        <InputNumber :min="-1" v-model="formItem.repeatCount"></InputNumber>
        <span style="margin-left: 5px"></span>
        <RadioGroup v-model="formItem.repeatCountType" @on-change="repeatCountTypeChange" type="button">
          <Radio label="0">不重复执行</Radio>
          <Radio label="-1">不限制次数,一直重复执行(直到过期)</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem v-if="formItem.jobType === 'simple'" label="重复执行间隔" prop="repeatInterval">
        <InputNumber :min="1000" v-model="formItem.repeatInterval"></InputNumber>
        <span><span style="margin-left: 5px"></span>毫秒</span>
      </FormItem>
      <FormItem v-if="formItem.jobType === 'cron'" label="cron表达式" prop="cron">
        <Input v-model="formItem.cron" placeholder="* * * * * ?"></Input>
      </FormItem>
      <FormItem label="服务名称" prop="serviceId">
        <Input :disabled="formItem.isEdit" v-model="formItem.serviceId" placeholder="serviceId"></Input>
      </FormItem>
      <FormItem label="接口地址" prop="path">
        <Input :disabled="formItem.isEdit" v-model="formItem.path" placeholder="/test"></Input>
      </FormItem>
      <FormItem label="请求方式" prop="method">
        <RadioGroup v-model="formItem.method" type="button">
          <Radio label="GET">GET</Radio>
          <Radio label="POST">POST</Radio>
          <Radio label="PUT">PUT</Radio>
          <Radio label="DELETE">DELETE</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="任务描述">
        <Input v-model="formItem.jobDescription" type="textarea" placeholder="请输入内容"></Input>
      </FormItem>
      <FormItem label="异常告警邮箱" prop="alarmMail">
        <Input v-model="formItem.alarmMail" placeholder="请输入内容"></Input>
      </FormItem>
    </Form>
  </div>
</template>

<script>
export default {
  name: 'JobInfo',
  data () {
    return {
      isEdit: false,
      selectApis: [],
      formItemRules: {
        jobName: [
          { required: true, message: '任务名称不能为空', trigger: 'blur' }
        ],
        jobType: [
          { required: true, message: '任务类型不能为空', trigger: 'blur' }
        ],
        cron: [
          { required: true, message: 'cron表达式不能为空', trigger: 'blur' }
        ],
        path: [
          { required: true, message: '接口地址不能为空', trigger: 'blur' }
        ],
        serviceId: [
          { required: true, message: '服务名称不能为空', trigger: 'blur' }
        ],
        method: [
          { required: true, message: '请求方式不能为空', trigger: 'blur' }
        ],
        alarmMail: [
          { required: false, type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空' }
        ],
        repeatInterval: [
          { required: true, message: '间隔时间不能为空' }
        ],
        repeatCount: [
          { required: true, message: '重试次数不能为空' }
        ]
      },
      formItem: this.getFormItem()
    }
  },
  methods: {
    getFormItem () {
      return {
        jobName: '',
        jobDescription: '',
        jobType: 'cron',
        cron: '',
        startTime: '',
        endTime: '',
        repeatInterval: 10000,
        repeatCountType: '0',
        repeatCount: 0,
        serviceId: '',
        path: '',
        method: 'GET',
        contentType: '',
        alarmMail: ''
      }
    },
    setData (data) {
      if (data && data.jobName && data.jobName !== '') {
        Object.assign(this.formItem, {
          jobName: data.jobName,
          jobDescription: data.jobDescription,
          jobType: data.jobType,
          cron: data.cron,
          startTime: data.startTime,
          endTime: data.endTime,
          repeatInterval: data.repeatInterval,
          repeatCountType: data.repeatCountType,
          repeatCount: data.repeatCount,
          serviceId: data.serviceId,
          path: data.path,
          method: data.method,
          contentType: data.contentType,
          alarmMail: data.alarmMail
        })
        this.formItem.jobType = this.formItem.jobTrigger === 'org.quartz.impl.triggers.SimpleTriggerImpl' ? 'simple' : 'cron'
        this.formItem.cron = data.cronExpression
        this.formItem.startTime = data.startDate
        this.formItem.endTime = data.endDate
        this.formItem.repeatInterval = data.repeatInterval ? parseInt(data.repeatInterval) : 0
        this.formItem.repeatCountType = data.repeatCount + ''
        this.formItem.path = data.data.path
        this.formItem.serviceId = data.data.serviceId
        this.formItem.method = data.data.method
        this.formItem.contentType = data.data.contentType
        this.formItem.alarmMail = data.data.alarmMail
        this.isEdit = true
      }
      this.handleLoadApiList()
    },
    handleReset () {
      this.$refs['form'].resetFields()
      this.formItem = this.getFormItem()
    },
    handleSubmit () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.formItem.jobType === 'simple') {
              this.formItem.startTime = this.formItem.startTime ? this.$utils.toDateString(this.formItem.startTime, 'yyyy-MM-dd HH:mm:ss') : ''
              this.formItem.endTime = this.formItem.endTime ? this.$utils.toDateString(this.formItem.endTime, 'yyyy-MM-dd HH:mm:ss') : ''
            }
            this.$http.post(this.$apis.jobs.save, this.formItem, this).then(res => {
              resolve(res)
            }).catch(function (err) {
              reject(err)
            })
          } else {
            resolve(false)
          }
        })
      })
    },
    handleLoadApiList () {
      this.$http.get(this.$apis.authority.api, {}, this).then(res => {
        res.data.map(item => {
          item.path = `${item.prefix.replace('/**', '')}${item.path}`
        })
        this.selectApis = res.data
      })
    },
    handleOnSelectChange (value) {
      let api = {}
      this.selectApis.some(item => {
        if (item.path === value) {
          api = item
          return true
        }
      })
      this.formItem.serviceId = api.serviceId
      this.formItem.path = api.path
      this.formItem.contentType = api.contentType
      this.formItem.method = api.requestMethod
    },
    repeatCountTypeChange (value) {
      this.formItem.repeatCount = parseInt(value)
    }
  },
  mounted: function () {
  }
}
</script>
